package net.ufrog.common.spring.app;

import net.ufrog.common.Logger;
import net.ufrog.common.app.App;
import net.ufrog.common.web.app.WebApp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.NoSuchMessageException;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * @author ultrafrog
 * @version 1.0, 2013-08-23
 * @since 1.0
 */
public class SpringWebApp extends WebApp {

    /** 容器上下文 */
    private static ApplicationContext applicationContext;

    /**
     * 构造函数
     *
     * @param request 请求
     * @param response 响应
     */
    public SpringWebApp(HttpServletRequest request, HttpServletResponse response) {
        super(request, response);
    }

    @Override
    public Locale getLocale() {
        Locale locale = Locale.class.cast(WebUtils.getSessionAttribute(this.request, SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME));
        if (locale == null) locale = this.request.getLocale();
        return locale;
    }

    @Override
    public String getMessage(String key, Object... args) {
        try {
            return String.format(applicationContext.getMessage(key, null, getLocale()), args);
        } catch (NoSuchMessageException e) {
            Logger.warn(e.getMessage());
            return key;
        }
    }

    /**
     * 读取实例
     *
     * @param name 名称
     * @return 实例
     */
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    /**
     * 读取实例
     *
     * @param name 名称
     * @param args 参数
     * @return 实例
     */
    public static Object getBean(String name, Object... args) {
        return applicationContext.getBean(name, args);
    }

    /**
     * 读取实例
     *
     * @param name 名称
     * @param requiredType 请求类型
     * @return 实例
     */
    public static <T> T getBean(String name, Class<T> requiredType) {
        return applicationContext.getBean(name, requiredType);
    }

    /**
     * 读取实例
     *
     * @param requiredType 请求类型
     * @return 实例
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    /**
     * 初始化
     *
     * @param context 上下文
     */
    public static void initialize(ServletContext context) {
        WebApp.initialize(context);
        applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        Logger.info("initialize application context: %s", applicationContext);
    }

    /**
     * 创建实例
     *
     * @param request 请求
     * @param response 响应
     * @return 当前线程应用实例
     */
    static App create(HttpServletRequest request, HttpServletResponse response) {
        current(new SpringWebApp(request, response));
        return current();
    }
}
